// pages/addorder/addorder.js
var Http = require('../../utils/http');
var Utils = require('../../utils/util');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    submitLoading: false,
    ajaxStatus: '',
    ajaxInfo: '',
    items: [
      { name: '电话', value: '1', checked: 'true' },
      { name: '短信/微信/其他', value: '2' }
    ],
    courseSelect: [
      // { pt_order_id: '111', course_name: '电话' }
    ],
    showData: {
      'member_name': '',
      'mobile': ''
    },
    formData: {
      order_id: '', // 预约id
      pt_id: '', // 教练id
      remarks: '', //内容备
      pt_order_id: '', // 	私教课订单id
      member_id: '', // 会员id
      order_date: '', // 上课日期 2017-11-11
      order_time: '', // 上课时间 11:11:11
      course_count: '', // 上课课时 5
      // shop_id: '' // 门店id
    }
  },
  radioChange(e) {
    this.setData({
      'formData.content_way': e.detail.value
    })
  },
  courseChanged(e) {
    this.setData({
      'formData.course_name': this.data.courseSelect[e.detail.value].course_name
    })
  },
  courseDateChange(e) {
    this.setData({
      'formData.order_date': e.detail.value
    })
  },
  courseTimeChange(e) {
    this.setData({
      'formData.order_time': e.detail.value
    })
  },
  courseCountInput(e) {
    this.setData({
      'formData.course_count': e.detail.value
    })
  },
  remarkInput(e) {
    this.setData({
      'formData.remarks': e.detail.value
    })
  },
  submitOrder() {
    this.setData({
      'submitLoading': true
    });
    const self = this;
    Http.post('/EditOrderPost', this.data.formData, (res) => {
      this.setData({
        'submitLoading': false
      });
      if (res.flag) {
        self.setData({
          'ajaxStatus': 'success',
          'ajaxInfo': res.msg
        });
        setTimeout(() => {
          wx.navigateBack({
            delta: 2
          })
        }, 2000);
      } else {
        self.setData({
          'ajaxStatus': 'error',
          'ajaxInfo': res.msg
        })
      }
    }, () => {
      this.setData({
        'submitLoading': true
      });
    });
  },
  initOrderDetail() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'formData.member_id': options.memberId,
      'formData.pt_order_id': options.ptOrderId,
      'formData.order_id': options.orderId
    });
    const self = this;
    Http.getLoginInfo((loginInfo) => {
      this.setData({
        'formData.pt_id': loginInfo.pt_id,
        'formData.shop_id': loginInfo.shop_id
      });
      Http.post('/EditOrder', {
        pt_id: loginInfo.pt_id,
        shop_id: loginInfo.shop_id,
        order_id: options.orderId,
        pt_order_id: options.ptOrderId,
      }, (res) => {
        if (res.flag) {
          self.setData({
            'showData.member_name': res.data.member_name,
            'showData.mobile': res.data.mobile,
            'formData.course_count': res.data.course_count,
            'formData.order_date': res.data.order_date,
            'formData.order_time': res.data.order_time,
            'formData.remarks': res.data.remarks
          });
        } else {
          wx.showToast({
            title: '初始化数据失败，请返回后重试',
            icon: 'none'
          })
        }
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  }
})
